import ErrorDialog
from etgui import *
from PyWizard import WizModel
from ShowHTML import ShowHTML,ShowDocu
import SetupWizModel,SetupWizStrings
reload(SetupWizModel)
reload(SetupWizStrings)
from SetupWizModel import SetupWizPage,AbstrCheckPage,AbstrCheckButtonPage,AbstrCheckButtonPage
from SetupWizModel import EnvVar,PathVar,EnvVarFile,EnvVarDir,WinPathVar
from SetupWizModel import ParserTool,LicenseTool,LicenseParserTool
from SetupWizModel import InstalledFile,GetLicenseDescription,ENVIRON
from SetupWizStrings import *
import regsub

class PageBegin(SWBeginPage,SetupWizPage):
	pref="Sniff.ShowBeginnersModeWizard"
	def DoMakeContent(self):
		return None
		self.choice=ManyOfCluster(self.s_dont_show_anymore)
		self.choice.SetItem(self.choice.GetId(),EnvGetBool(self.pref,1))
		return self.choice
	def Control(self,id,part,val):
		return 1
		sel=self.choice.IsItemOn(self.choice.GetId())
		if (sel==0) != (EnvGetBool(self.pref,1)==0):
			if not sel:
				ShowAlert(eAlertNote,self.s_alert)
			EnvSetBool(self.pref,sel)
			EnvSave(1)
		return 1
	def EditEnd(self):
		pass

class PageCheckEnv(SWCheckEnv,AbstrCheckButtonPage):
	def GetCheckedItems(self):
		return self.model.environ

class PageCheckTools(SWCheckTools,AbstrCheckButtonPage):
	def GetCheckedItems(self):
		return self.model.tools

class PageCheckLicenses(SWCheckLicenses,AbstrCheckButtonPage):
	def MakeDescription(self):
		return GetLicenseDescription()
	def GetCheckedItems(self):
		return self.model.licenses

class PageCheckExamples(SWCheckExamples,AbstrCheckButtonPage):
	def GetCheckedItems(self):
		return self.model.examples

class PageCheckDocu(SWCheckDocu,AbstrCheckButtonPage):
	def GetCheckedItems(self):
		return self.model.docs
 
class PageStartTutorial(SWStartTutorial,SetupWizPage):
	def DoMakeContent(self):
		list=[self.s_no_tutorial]
		i=10000
		tools=self.model.GetToolsWithTutorial()
		for t in tools:
			#item=RadioButton(i,TextItem(t.name))
			item=RadioButton(i,TextItem(t.name))
			enable=0
			if t.installed and self.model.HasLicense(t.name):
				if self.model.doc.Check():
					enable=1
			item.Enable(enable)
			list.append(item)
			i=i+1
		self.choice=apply(OneOfCluster, tuple(list))
		return self.choice
		return BorderItem(self.s_which_tutorial,self.choice)
	def EditEnd(self):
		sel=self.choice.GetCurrentItem()-10000
		if sel>=0:
			self.model.tutorial=self.model.GetToolsWithTutorial()[sel]
		else:
			self.model.tutorial=None
class PageFinish(SWFinish,SetupWizPage):
	def DoMakeContent(self):
		self.box=VBox(items=(TextItem("Done"),))
		return self.box
	def EditBegin(self):
		t=self.model.tutorial
		if t:
			self.SetText(self.s_show_tutorial% t.name)
		else:
			self.SetText(self.s_no_tutorial)
	def SetText(self,str):
		self.box.SetAt(0,MultilineTextItem(str))

class Model(SWModel,WizModel):
	tutorial=None
	can_finish=1
	showpages=1
	environ=licenses=tools=examples=doc=()
	def __init__(self):
		#WizModel.__init__(self)
		self.CheckEnvironment()
		self.CheckTools()
		self.CheckLicences()
		self.CheckFiles()
	def EverythingOK(self):
#		for l in self.environ,self.licenses,self.tools,self.examples,self.doc:
		for l in self.environ,self.licenses:
			for check in l:
				if not check.IsOk():
					return 0
		return 1
	def CheckEnvironment(self):
		if IsWindows():
			sniffdir=GetSniffDir()
			wsniffdir=regsub.gsub('/','\\',sniffdir)
			self.environ=(
				EnvVarDir("SNIFF_DIR"),
				WinPathVar("PATH",wsniffdir+"\\bin"),
				EnvVarFile("SHELL",value=wsniffdir+"\\bin\\sh.exe"),
				EnvVarFile("ENV",value=wsniffdir+"\\config\\environ.ksh"),
			)
		else:
			self.environ=(
				EnvVarDir("SNIFF_DIR"),
				PathVar("PATH"),
				EnvVarDir("HOME"),
			)
			
	def CheckTools(self):
		self.tools=(
			ParserTool(
				self.s_tool_cpp,
				"sniffparser",
				"doc/online/cpp"
			),
			ParserTool(
				self.s_tool_java,
				"sniffjava",
				"doc/online/java"
			),
			ParserTool(
				self.s_tool_fortran,
				"sniff_fortran",
				"doc/online/fortran"
			),
			ParserTool(
				self.s_tool_idl,
				"sniffidl"
			),
		)
	def CheckLicences(self):
		self.licenses=(
			LicenseTool("SNiFF",self.s_tool_sniff),
			LicenseParserTool("C++",self.s_tool_cpp),
			LicenseParserTool("Java",self.s_tool_java),
			LicenseParserTool("Fortran",self.s_tool_fortran),
			LicenseParserTool("IDL",self.s_tool_idl),
			#LicenseTool("ZZZ"),
		)
	def CheckFiles(self):
		self.example=InstalledFile(self.s_installed_examples,"example")
		self.doc=InstalledFile(self.s_installed_docu,"doc/online")
		self.examples=[self.example]
		self.docs=[self.doc]

	def HasLicense(self,name):
		for l in self.licenses:
			if l.name==name:
				return l.value!=None
	def CanFinish(self):
		return self.can_finish
	def Finish(self):
		if self.tutorial and self.tutorial.tutorial:
			ShowDocu(self.tutorial.tutorial)
		#import PrettyPrinter
		#PrettyPrinter.Print(self)
		global gWizard
		gWizard=None
		return 1
	def Cancel(self):
		global gWizard
		gWizard=None
	def __getitem__(self,name):
		return getattr(self,name)
	def GetToolsWithTutorial(self):
		l=[]
		for t in self.tools:
			if t.tutorial:
				l.append(t)
		return l
def MakePages(select_tutorial=1):
	pages=[]
	task=WizTask(SWTasks.s_begin,eTaskBegin)
	pages.append(PageBegin(task))
	task=WizTask(SWTasks.s_check_env)
	pages.append(PageCheckEnv(task))
	task=WizTask(SWTasks.s_check_tools)
	pages.append(PageCheckTools(task))
	task=WizTask(SWTasks.s_check_licenses)
	pages.append(PageCheckLicenses(task))
	task=WizTask(SWTasks.s_select_tutorial)
	pages.append(PageCheckExamples(task))
	task=WizTask(SWTasks.s_check_examples)
	pages.append(PageCheckDocu(task))
	task=WizTask(SWTasks.s_check_docu)
	if select_tutorial:
		pages.append(PageStartTutorial(task))
	pages.append(PageFinish(WizTask(SWTasks.s_finish,eTaskFinish)))
	return pages
gWizard=None
gIgnoreLicenseErrors=0
def Show(force,select_tutorial=1):
	if not force:
		return
	global gWizard,gInWizard
	global gIgnoreLicenseErrors
	gInWizard=1
	if gWizard:
		if gWizard!=1:
			gWizard.ShowUnderMouse()
	else:
		gWizard=1
		gIgnoreLicenseErrors=1
		model=Model()
		gIgnoreLicenseErrors=0
		if force or not model.EverythingOK():
			pages= MakePages(select_tutorial)
			gWizard= ShowWizard(pages,model,eWinFixed)
		else:
			gWizard=None
	gInWizard=0
gNoLicWizard=None
gInWizard=0
gModalWizard=None
def ShowModal():
	global gModalWizard,gInWizard
	global gIgnoreLicenseErrors
	gInWizard=1
	if gModalWizard:
		gModalWizard.ShowUnderMouse()
	else:
		gIgnoreLicenseErrors=1
		model=Model()
		gIgnoreLicenseErrors=0
		pages= MakePages(0)
		gModalWizard= ShowWizard(pages,model,eWinBlock)
	gInWizard=0


class NoLicenseModel(Model):
	def __init__(self):
		#WizModel.__init__(self)
		self.CheckLicences()
	def Finish(self):
		global gNoLicWizard
		gNoLicWizard=None
		return 1

def MakeNoLicPages():
	pages=[]
	task=WizTask(SWTasks.s_begin,eTaskBegin)
	pages.append(PageBegin(task))
	task=WizTask(SWTasks.s_check_licenses)
	pages.append(PageCheckLicenses(task))
	pages.append(PageFinish(WizTask(SWTasks.s_finish,eTaskFinish)))
	return pages

gNoLicWizard=None
def ShowNoLicense(tool,msg):
	if gIgnoreLicenseErrors:
		return
	##print "***",tool,msg
	global gNoLicWizard
	global gIgnoreLicenseErrors
	if gNoLicWizard:
		if gNoLicWizard!=1:
			gNoLicWizard.ShowUnderMouse()
		return
	gNoLicWizard=1
	gIgnoreLicenseErrors=1
	model=NoLicenseModel()
	gIgnoreLicenseErrors=0
	pages= MakeNoLicPages()
	gNoLicWizard= ShowWizard(pages,model,eWinFixed)
